package cn.wzy.service.impl;


import cn.wzy.enums.AppHttpCodeEnum;
import cn.wzy.mapper.MenuMapper;
import cn.wzy.pojo.Employee;
import cn.wzy.pojo.Menu;

import cn.wzy.results.ResponseResult;
import cn.wzy.service.MenuService;
import cn.wzy.utils.RedisCache;
import cn.wzy.vo.GoPageVo;
import cn.wzy.vo.PageVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * (Menu)表服务实现类
 *
 * @author makejava
 * @since 2022-12-08 13:53:15
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class MenuServiceImpl extends ServiceImpl<MenuMapper,Menu>implements MenuService {
    @Resource
    private MenuMapper menuMapper;
    @Resource
    private RedisCache redisCache;

    /*
    * 菜单分页
    * */
    @Override
    public ResponseResult menuPage(GoPageVo goPageVo) {
        Long stater = (goPageVo.getPageIndex()-1)* goPageVo.getPageSize();
//        总数
        Long total = menuMapper.getTotal(goPageVo);
//        分页
        List<Menu> menuList = menuMapper.menuPage(goPageVo.getKeyword(),stater,goPageVo.getPageSize());
//          封装
        PageVo pageVo = new PageVo(menuList,total,goPageVo.getPageSize());
        return ResponseResult.okResult(pageVo);
    }

    /*
    * 单个删除
    * */
    @Override
    @Transactional
    public ResponseResult deleteMenu(Long id) {
//        判断是否有下级菜单
        List<Menu> menuList = menuMapper.getLowerMenu(id);
        if (Objects.isNull(menuList) || menuList.size()<1){
//            删除
            menuMapper.deleteMenuById(id);
            return ResponseResult.okResult();
        }
//        有下级菜单不能删除
        return ResponseResult.errorResult(AppHttpCodeEnum.MENU_DELETE);
    }

    /*
    * 批量删除
    * */
    @Override
    @Transactional
    public ResponseResult deleteListMenu(List<Long> ids) {
        if (Objects.isNull(ids)||ids.size()<1){
            return ResponseResult.errorResult(400,"没有可删除数据");
        }
        for (Long id : ids) {
            //        判断是否有下级菜单
            List<Menu> menuList = menuMapper.getLowerMenu(id);
            if (!Objects.isNull(menuList)&&menuList.size()>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.MENU_DELETE);
            }
        }
//        删除菜单
        menuMapper.deleteMenuList(ids);
        return ResponseResult.okResult();
    }


    /*
    * 添加/修改
    * */
    @Override
    @Transactional
    public ResponseResult saveMenu(Menu menu) {
//        通过id来判断是否添加/修改
        if (Objects.isNull(menu.getId())){
//            添加
            menuMapper.addMenu(menu);
            return ResponseResult.okResult();
        }
//        修改
        menuMapper.updateMenu(menu);
        return ResponseResult.okResult();
    }


    /*
    * 多级菜单
    * */
    @Override
    public ResponseResult getMenuLevel() {
        List<Menu> menuList = menuMapper.getMenuLevel();
        return ResponseResult.okResult(menuList);
    }

    /*
    * 查询所有菜单id
    * */
    @Override
    public ResponseResult getAllMenuIds() {
        List<Long> ids = menuMapper.getAllMenuIds();
        return ResponseResult.okResult(ids);
    }

    /*
    * 通过EmpId动态获取到菜单分页
    * */
    @Override
    public ResponseResult dynamicPageMenu(GoPageVo goPageVo, HttpServletRequest request) {

        //          从rides中获取到用户id
        String token = request.getHeader("token");
        Employee employee = redisCache.getCacheObject("User:" + token);
        Long empId = employee.getId();


        if("获取树".equals(goPageVo.getKeyword())){

            List<Menu> menuList = menuMapper.getPageMenuTreeByEmpId(empId);
            return ResponseResult.okResult(menuList);

        }else {
            //        获取总数
            Long total = menuMapper.getPageMenuByEmpIdTotal(goPageVo.getKeyword(),empId);
            Long stater = (goPageVo.getPageIndex()-1)* goPageVo.getPageSize();
            List<Menu> menuList = menuMapper.getPageMenuByEmpId(goPageVo.getKeyword(),stater,goPageVo.getPageSize(),empId);
            PageVo pageVo = new PageVo(menuList, total, goPageVo.getPageSize());
            return ResponseResult.okResult(pageVo);
        }

    }
}
